
package com.sl.au.service.impl;

import java.util.List;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.BusinesssellerAuthentication;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.repository.BusinesssellerAuthenticationRepository;
import com.sl.au.service.BusinesssellerAuthenticationService;
import com.sl.au.service.OptionService;
import com.sl.au.repository.BusinesssellerBaseInfoRepository;

@Service("BusinesssellerAuthenticationService")
@Transactional
public class BusinesssellerAuthenticationServiceImp extends BaseServiceImpl implements BusinesssellerAuthenticationService{

	@Autowired
	BusinesssellerAuthenticationRepository businesssellerAuthenticationRepository;
	
	@Autowired
	BusinesssellerBaseInfoRepository businesssellerBaseInfoRepository;
	@Autowired
	OptionService optionService;
	
	public BusinesssellerAuthenticationRepository getRepository()
	{
		return this.businesssellerAuthenticationRepository;
	}
	
	@Override
	public List<BusinesssellerAuthentication> getPage(int start, int length, String key, String orderName, Direction dir) {
		Page<BusinesssellerAuthentication> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if(StringUtils.hasText(key)){
			pages=businesssellerAuthenticationRepository.getByKey(key,"初始化",pageable);
		}
		else{
			pages = businesssellerAuthenticationRepository.getByCheckKey("初始化", pageable);//.findAll(pageable);
		}
		
		return pages.getContent();
	}
	@Override
	public int getCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if (!StringUtils.hasText(key))
			return businesssellerAuthenticationRepository.getByCheckKey("初始化", pageable).getContent().size();
		
		return businesssellerAuthenticationRepository.getByKey(key,"初始化", pageable).getContent().size();
	}
	@Override
	public int rejectCheck(String id,User user,String memo)
	{
		int code =-1;
		try{
			BusinesssellerAuthentication businesssellerAuthentication=this.getRepository().getInfoById(id);
			businesssellerAuthentication.setCheckPerson(user);
			businesssellerAuthentication.setMemo(memo);
			List<Option> option =optionService.getRepository().findByGroupAndTitle("信息状态", "认证拒绝");
			if(option.size()==0)
			{
				code=-1;
			}
			else{
				businesssellerAuthentication.setInfoState(option.get(0));
				businesssellerAuthentication.setCheckDate(DateTime.now().toDate());
				this.getRepository().save(businesssellerAuthentication);
				code=1;
			}
			
		}
		catch(Exception ex)
		{
			code=-1;
		}
		return code;
	}

	@Override
	public int rejectCheck(String id,User user,String memo,double lng,double lat)
	{
		int code =-1;
		try{
			BusinesssellerAuthentication businesssellerAuthentication=this.getRepository().getInfoById(id);
			businesssellerAuthentication.setCheckPerson(user);
			businesssellerAuthentication.setMemo(memo);
			businesssellerAuthentication.setLongitude(lng);
			businesssellerAuthentication.setLatitude(lat);
			List<Option> option =optionService.getRepository().findByGroupAndTitle("信息状态", "认证拒绝");
			if(option.size()==0)
			{
				code=-1;
			}
			else{
				businesssellerAuthentication.setInfoState(option.get(0));
				businesssellerAuthentication.setCheckDate(DateTime.now().toDate());
				this.getRepository().save(businesssellerAuthentication);
				code=1;
			}
			
		}
		catch(Exception ex)
		{
			code=-1;
		}
		return code;
	}
	
	@Override
	public int saveAuthenticationDetail(BusinesssellerAuthentication ba,String state)
	{
	  int code=-1;	
	  
	  List<Option> option =optionService.getRepository().findByGroupAndTitle("信息状态", state);
	  
		if(option.size()==0)
		{
			code=-1;
		}
		else{
			BusinesssellerAuthentication temp =this.businesssellerAuthenticationRepository.findOne(ba.getId());
			
			if(ba.getPictureAdress()==null||ba.getPictureAdress()==""){
				ba.setPictureAdress(temp.getPictureAdress());
			}
			
			if(temp.getInfoState().getTitle().equals("待认证"))
			{
				ba.setCleaningPerson(temp.getCleaningPerson());
				ba.setManagementRange(temp.getManagementRange());
				//ba.setPictureAdress(temp.getPictureAdress());
				ba.setShopAdress(temp.getShopAdress());
				ba.setSignPerson(temp.getSignPerson());
				
			}
			else{
			ba.setInfoState(option.get(0));
			}
			
			ba.setUpdateTime(DateTime.now().toDate());
			ba.setManagementPerson(temp.getManagementPerson());
			ba.setCreateTime(temp.getCreateTime());
			ba.setCheckDate(temp.getCheckDate());
			ba.setCheckPerson(temp.getCheckPerson());
			
			BusinesssellerBaseInfo businesssellerBaseInfo=ba.getBusinesssellerBaseInfo();
			businesssellerBaseInfo.setAreaMeasure(ba.getAreaMeasure());
			//businesssellerBaseInfo.setCleaningPerson(ba.getCleaningPerson());
			businesssellerBaseInfo.setFileAdress(ba.getFileAdress());
			businesssellerBaseInfo.setInfoState(ba.getInfoState());
			businesssellerBaseInfo.setLatitude(ba.getLatitude());
			businesssellerBaseInfo.setLongitude(ba.getLongitude());
			businesssellerBaseInfo.setManagementPerson(ba.getManagementPerson());
			//businesssellerBaseInfo.setManagementRange(ba.getManagementRange());
			//businesssellerBaseInfo.setShopAdress(ba.getShopAdress());
			businesssellerBaseInfo.setShopName(ba.getShopName());
			//businesssellerBaseInfo.setSignPerson(ba.getSignPerson());
			businesssellerBaseInfo.setUpdateTime(ba.getUpdateTime());
			businesssellerBaseInfo.setPictureAdress(ba.getPictureAdress());
			this.getRepository().save(ba);
			businesssellerBaseInfoRepository.save(businesssellerBaseInfo);
			code=1;
		}
	  
	  return code;
	}
	@Override
	public int accessCheck(String id,User user,String memo)
	{
		int code =-1;
		try{
			BusinesssellerAuthentication businesssellerAuthentication=this.getRepository().getInfoById(id);
			businesssellerAuthentication.setCheckPerson(user);
			businesssellerAuthentication.setMemo(memo);
			List<Option> option =optionService.getRepository().findByGroupAndTitle("信息状态", "认证通过");
			if(option.size()==0)
			{
				code=-1;
			}
			else{
				businesssellerAuthentication.setInfoState(option.get(0));
				businesssellerAuthentication.setCheckDate(DateTime.now().toDate());
				
				BusinesssellerBaseInfo businesssellerBaseInfo=businesssellerAuthentication.getBusinesssellerBaseInfo();
				businesssellerBaseInfo.setAreaInfo(businesssellerAuthentication.getAreaInfo());
				businesssellerBaseInfo.setArreaManagementPerson(businesssellerAuthentication.getArreaManagementPerson());
				businesssellerBaseInfo.setAreaMeasure(businesssellerAuthentication.getAreaMeasure());
				businesssellerBaseInfo.setCleaningPerson(businesssellerAuthentication.getCleaningPerson());
				businesssellerBaseInfo.setFileAdress(businesssellerAuthentication.getFileAdress());
				businesssellerBaseInfo.setInfoState(businesssellerAuthentication.getInfoState());
				businesssellerBaseInfo.setLatitude(businesssellerAuthentication.getLatitude());
				businesssellerBaseInfo.setLongitude(businesssellerAuthentication.getLongitude());
				businesssellerBaseInfo.setManagementPerson(businesssellerAuthentication.getManagementPerson());
				businesssellerBaseInfo.setManagementRange(businesssellerAuthentication.getManagementRange());
				businesssellerBaseInfo.setPictureAdress(businesssellerAuthentication.getPictureAdress());
				businesssellerBaseInfo.setShopAdress(businesssellerAuthentication.getShopAdress());
				businesssellerBaseInfo.setShopName(businesssellerAuthentication.getShopName());
				businesssellerBaseInfo.setSignPerson(businesssellerAuthentication.getSignPerson());
				businesssellerBaseInfo.setUpdateTime(businesssellerAuthentication.getUpdateTime());
				businesssellerBaseInfo.setMemo(businesssellerAuthentication.getMemo());
				this.getRepository().save(businesssellerAuthentication);
				businesssellerBaseInfoRepository.save(businesssellerBaseInfo);
				code=1;
			}
			
		}
		catch(Exception ex)
		{
			code=-1;
		}
		return code;
	}

	@Override
	public int accessCheck(String id,User user,String memo,double lng,double lat)
	{
		int code =-1;
		try{
			BusinesssellerAuthentication businesssellerAuthentication=this.getRepository().getInfoById(id);
			businesssellerAuthentication.setCheckPerson(user);
			businesssellerAuthentication.setMemo(memo);
			businesssellerAuthentication.setLatitude(lat);
			businesssellerAuthentication.setLongitude(lng);
			List<Option> option =optionService.getRepository().findByGroupAndTitle("信息状态", "认证通过");
			if(option.size()==0)
			{
				code=-1;
			}
			else{
				businesssellerAuthentication.setInfoState(option.get(0));
				businesssellerAuthentication.setCheckDate(DateTime.now().toDate());
				
				BusinesssellerBaseInfo businesssellerBaseInfo=businesssellerAuthentication.getBusinesssellerBaseInfo();
				businesssellerBaseInfo.setAreaInfo(businesssellerAuthentication.getAreaInfo());
				businesssellerBaseInfo.setArreaManagementPerson(businesssellerAuthentication.getArreaManagementPerson());
				businesssellerBaseInfo.setAreaMeasure(businesssellerAuthentication.getAreaMeasure());
				businesssellerBaseInfo.setCleaningPerson(businesssellerAuthentication.getCleaningPerson());
				businesssellerBaseInfo.setFileAdress(businesssellerAuthentication.getFileAdress());
				businesssellerBaseInfo.setInfoState(businesssellerAuthentication.getInfoState());
				businesssellerBaseInfo.setLatitude(businesssellerAuthentication.getLatitude());
				businesssellerBaseInfo.setLongitude(businesssellerAuthentication.getLongitude());
				businesssellerBaseInfo.setManagementPerson(businesssellerAuthentication.getManagementPerson());
				businesssellerBaseInfo.setManagementRange(businesssellerAuthentication.getManagementRange());
				businesssellerBaseInfo.setPictureAdress(businesssellerAuthentication.getPictureAdress());
				businesssellerBaseInfo.setShopAdress(businesssellerAuthentication.getShopAdress());
				businesssellerBaseInfo.setShopName(businesssellerAuthentication.getShopName());
				businesssellerBaseInfo.setSignPerson(businesssellerAuthentication.getSignPerson());
				businesssellerBaseInfo.setUpdateTime(businesssellerAuthentication.getUpdateTime());
				businesssellerBaseInfo.setMemo(businesssellerAuthentication.getMemo());
				businesssellerBaseInfo.setLatitude(businesssellerAuthentication.getLatitude());
				businesssellerBaseInfo.setLongitude(businesssellerAuthentication.getLongitude());
				this.getRepository().save(businesssellerAuthentication);
				businesssellerBaseInfoRepository.save(businesssellerBaseInfo);
				code=1;
			}
			
		}
		catch(Exception ex)
		{
			code=-1;
		}
		return code;
	}
}
